<template>
    <!-- 事件绑定到了元素 -->
    <div v-if="isExternals" :style="styleExternalIcon" class="svg-icon svg-external-icon" v-on="$listeners" />
    <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
        <use :href="iconName" />
    </svg>
</template>

<script>
    // 外链验证
    import { isExternal } from '@/utils';

    export default {
        name: 'SvgIcon',
        props: {
            iconClass: {
                type: String,
                required: true
            },
            className: {
                type: String,
                default: ''
            }
        },
        computed: {
            isExternals() {
                return isExternal(this.iconClass);
            },
            iconName() {
                return `#icon-${this.iconClass}`;
            },
            // 增强 svg 图标样式
            svgClass() {
                if (this.className) {
                    return 'svg-icon ' + this.className;
                } else {
                    return 'svg-icon';
                }
            },
            styleExternalIcon() {
                return {
                    mask: `url(${this.iconClass}) no-repeat 50% 50%`,
                    '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
                };
            }
        }
    };
</script>

<style lang="less" scoped>
    /* 基本样式 */
    .svg-icon {
        width: 1em;
        height: 1em;
        /*vertical-align: -0.15em;*/
        vertical-align:middle;
        fill: currentColor;
        overflow: hidden;
    }
    /* 外链图片增强 */
    .svg-external-icon {
        background-color: currentColor;
        mask-size: cover!important;
        display: inline-block;
    }
</style>